package ui;

import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.StringItem;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Command;

import manager.AgentManagerMIDlet;

public class IdleScreen extends Form implements CommandListener{
	private Command confirmCommand;
	private Command exitCommand;
	private Command createAgentCommand;
	private Command refreshCommand;
	private Command handleIncomingAgentCommand;
	private AgentManagerMIDlet midlet;
	private Display display;
    private StringItem agentDisplay;
    private manager.IOHandlerManager ioHandler;
 //   private String agent;
    private ap.MeetingAgent agent;
	
	public IdleScreen(AgentManagerMIDlet midlet,Display display){
		super("Waiting to receive an agent.");
		this.display = display;
		this.midlet = midlet;
        confirmCommand = new Command("Confirm", Command.SCREEN,1);
        exitCommand = new Command("Exit", Command.EXIT, 1);
        createAgentCommand = new Command("Create new agent and send", Command.SCREEN,1);
        refreshCommand = new Command("Refresh",Command.SCREEN,1);
        this.addCommand(createAgentCommand);
        this.addCommand(confirmCommand);
        this.addCommand(exitCommand);
        this.addCommand(refreshCommand);
        this.setCommandListener(this);
        this.ioHandler = midlet.getIOHandlerManager();
        //Thread checkForAgents = new Thread(new AgentChecker(ioHandler,this));
        //checkForAgents.start();
    }
	
//	public void displayMobileAgent(String agent){
//		agentDisplay= new StringItem("Agent is : ",agent);
//		this.append(agentDisplay);
//	}
	
	public void commandAction(Command c, Displayable d){
		if(c==exitCommand){
			midlet.exitRequested();
		}else if (c==confirmCommand){
			
		}else if (c==createAgentCommand){
			AgentCreationScreen agentCreationScreen = 
				new AgentCreationScreen(midlet, "Agent Creation", display);
			display.setCurrent(agentCreationScreen);
		}else if(c==refreshCommand){
			agent = ioHandler.receiveAgent();
			if(agent != null){
				agentDisplay= new StringItem("Agent is : ",agent.toString());
				this.append(agentDisplay);
				handleIncomingAgentCommand = new Command("Handle incoming Agent",Command.SCREEN,1);
				this.addCommand(handleIncomingAgentCommand);
			}
		}else if(c==handleIncomingAgentCommand){
			ap.MeetingAgent tempAgent = new ap.MeetingAgent();
			tempAgent.fromString(agent.toString());
			/* If not last contact in list. */
			if(tempAgent.getNextIndex()<tempAgent.getLastContactIndex()){
				ReceiveAgentScreen receiveAgentScreen = new ReceiveAgentScreen(midlet,display,tempAgent);
				display.setCurrent(receiveAgentScreen);
			}else{
				/*Last person in contacts, so display results to original sender.*/
				String availabilityDisplay = "";
				//int count=0;
				/* Display availability of agent*/
				for(int i = 0 ; i < tempAgent.getLastContactIndex() ; i++){
					int count = 0;
					for(int j = 1 ; j < tempAgent.getLastTimeIndex() ; j++){
						if(tempAgent.getAvailability(i, j)){
							count++;
						}
					}
					availabilityDisplay = availabilityDisplay + tempAgent.getStartTimes(i) 
																+ tempAgent.getFinishTimes(i)
																+ count;
				}
				agentDisplay = new StringItem("Agent returned results : ", availabilityDisplay);
				this.append(agentDisplay);
			}
		}
	}
}
